<!--
 * @Author: XieQiu 雪球
 * @Date: 2021-05-31 15:07:49
 * @LastEditTime: 2023-07-10 13:08:47
 * @LastEditors: Please set LastEditors
 * @Description: 仓库管理页面
-->
<template>
  <BaseMainSlot :show-btn-box="false">
    <template v-slot:btn-box>
      <ActionBar
        :page-no="115"
        :add-active="addActive"
        :del-active="delActive"
        :edit-active="editActive"
        :cancel-active="cancelActive"
        :save-active="saveActive"
        :has-edit="false"
        :has-save="false"
        :has-cancel="false"
        :has-del="false"
        :has-add="false"
        @add="add"
        @cancel="cancel('formData')"
        @edit="edit"
        @del="del"
        @save="save('formData')"
      />
    </template>
    <template v-slot:search-box>
      <SearchForm :formConfig="formConfig" :formData="searchlist" @search="search"></SearchForm>
    </template>
    <template v-slot:table-box>
      <el-table
        ref="tableData"
        v-loading="loading"
        :data="tableData"
        stripe
        border
        height="88%"
        :row-class-name="tableRowClassName"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="50" />
        <el-table-column prop="warehouseName" label="仓库名称" />
        <!-- <el-table-column
          prop="warehouseType.warehouseTypeName"
          label="仓库类型"
        /> -->
        <el-table-column prop="warehouseTypeName" label="仓库类型" />
        <el-table-column prop="warehouseArea" label="仓储面积">
          <template v-if="scope.row.warehouseArea" slot-scope="scope">
            {{ scope.row.warehouseArea }}m²
          </template>
        </el-table-column>
        <el-table-column prop="warehousePosition" label="仓库位置" />
        <el-table-column width="120" label="操作">
          <template v-if="scope.row.warehouseId" slot-scope="scope">
            <el-button v-if="judgePermission(115, 'edit')" class="text-btn-cursor" type="text" size="small" @click="handleEdit(scope.row)"><i class="el-icon-edit-outline"></i> 修改</el-button>
            <!-- <el-button class="text-btn-cursor cursor-delete" type="text" size="small" @click="handleDelete(scope.row)"><i class="el-icon-delete"></i> 删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog :title="dialogTitle + '仓库信息'" :visible.sync="dialogFormVisible" :before-close="cancel">
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          :rules="rules"
          class="el-form"
        >
          <el-form-item label="仓库类型:" prop="warehouseTypeName" class="w100p">
            <el-select
              v-model="formData.warehouseTypeName"
              :disabled="!canInput&& nowChanceStau === 'edit'"
              clearable
              filterable
            >
              <el-option
                v-for="item in warehouseTypeArr"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="仓库名称:" prop="warehouseName" class="w100p">
            <el-input
              v-model="formData.warehouseName"
              :disabled="!canInput&& nowChanceStau === 'edit'"
              placeholder="请输入仓库名称"
              clearable
            />
          </el-form-item>
          <el-form-item label="仓储面积:" class="w100p">
            <el-input
              v-model="formData.warehouseArea"
              placeholder="请输入仓储面积(数字)"

              clearable
            >
              <template slot="append">
                平方米(m²)
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="仓库位置:" class="w100p">
            <el-input
              v-model="formData.warehousePosition"
              placeholder="请输入仓库位置"

              clearable
            />
          </el-form-item>
          <el-form-item label="仓库存放上限:" class="w100p">
            <el-input
              v-model="formData.warehouseLimit"
              placeholder="请输入仓库存放上限"

              clearable
            />
          </el-form-item>
          <el-form-item label="备注:" class="w100p">
            <el-input
              v-model="formData.remark"
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4 }"

              clearable
            />
          </el-form-item>
        </el-form>
        <div v-if="nowChanceStau !== ''" slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel('formData')">
            取 消
          </el-button>
          <el-button class="zh-button-two" type="primary" @click="save('formData')">
            保 存
          </el-button>
        </div>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
import SearchForm from '@/components/searchForm';
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import warehouseManage from "@/api/basic-info-manage/warehouse-manage";
// import warehouseTypeManage from "@/api/basic-info-manage/warehouse-type-manage";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot";
import { mymixin } from "@/utils/mixin";
import { mapGetters } from 'vuex';
export default {
  name: "WarhouseManage",
  components: { SearchForm, ActionBar, Pagination, BaseMainSlot },
  mixins: [mymixin],
  data() {
    return {
      formConfig: [
        {
          label: '仓库名称',
          prop: 'warehouseName',
          type: 'input',
        },
      ],
      searchlist: {},
      // 是否能输入
      // canInput: false,
      //  仓库类型数组
      warehouseTypeArr: [],
      // 选中的数据项数组 ID组成
      warehouseIdList: [],
      rules: {
        warehouseName: [{ required: true, message: '请输入仓库名称', trigger: 'blur' }],
        warehouseTypeId: [{ required: true, message: '请选择仓库类型', trigger: 'change' }]
      },
      dialogTitle: "",
      dialogFormVisible: false
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {
    // 监听英文名只能输入英文和数字
    // formData: {
    //   // handler(val) {
    //   //   if (val.warehouseArea.replace(/[^\d]/g)) {
    //   //     //  console.log(val)
    //   //     this.$message.warning('仓储面积只能输入数字')
    //   //   }
    //   // }
    // }
  },
  created() {
    //  获取仓库类型
    this.queryWareHouseTypeList();
  },
  mounted() {
    // 获取表格数据
    this.getTableDataList();
  },
  methods: {
    getTableDataList() {
      this.loading = true;
      let param = {
        page: this.currentPage,
        size: this.pageSize,
        organizationNo: sessionStorage.organizationNo,
        entity: {
          warehouseName: this.searchlist.warehouseName
        }
      };
      warehouseManage.getWareHousingList(param).then(res => {
        this.loading = false;
        let data = res.data;
        // // 赋值总条数
        // console.log(data)
        if (data) {
          this.totalSize = data.totalElements;
          // // 赋值数组
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },
    // 查询所有仓库类型
    async queryWareHouseTypeList() {
      const { data: resp } = await warehouseManage.getTypeList();
      this.warehouseTypeArr = resp;
    },
    search() {
      this.currentPage = 1;
      this.getTableDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    // 表格行index
    tableRowClassName({ row, rowIndex }) {
      row.index = rowIndex;
      return "rows";
    },
    add() {
      this.formData = {};
      this.openDialog('add');
    },
    handleEdit(row) {
      // TODO 点击行元素后 edit
      this.formData = JSON.parse(JSON.stringify(row));
      this.editActive = true;
      this.saveActive = false;
      this.cancelActive = true;
      // this.$refs.formData.resetFields();
      this.canInput = false;
      this.openDialog('edit');
    },
    cancel() {
      this.formData = {};
      this.closeDialog();
    },
    handleSelectionChange(list) {
      // console.log(list)
      this.warehouseIdList = [];
      this.warehouseIdList = list.map(item => item.warehouseId);
      if (this.warehouseIdList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    delmessage() {
      warehouseManage.delWareHouseInfo(this.warehouseIdList).then(res => {
        if (res.code === 1) {
          this.$message.success(res.msg);
          this.$refs.formData.resetFields();
          this.getTableDataList();
        } else {
          this.$message.warning(res.msg || '删除失败');
        }
      });
    },
    handleDelete(row) {
      warehouseManage.delWareHouseInfo([row.warehouseId]).then(res => {
        if (res.code === 1) {
          this.$message.success(res.msg);
          this.$refs.formData.resetFields();
          this.getTableDataList();
        } else {
          this.$message.warning(res.msg || '删除失败');
        }
      });
    },
    save(formData) {
      this.$refs[formData].validate((valid) => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            // 编辑接口
            if (this.formData.warehouseLimit) {
              this.formData.warehouseLimit = JSON.parse(this.formData.warehouseLimit)
            }
            warehouseManage.editWareHouseInfo(this.formData).then(res => {
              if (res.code === 1) {
                this.$message.success(res.msg || "修改成功！");
                this.closeDialog();
                this.$refs[formData].resetFields();
                this.getTableDataList();
              } else {
                this.$message.error(res.msg || "修改失败！");
              }
            });
          } else if (this.nowChanceStau === "add") {
            // 新增接口
            // console.log(this.formData)
            warehouseManage.addWareHouseInfo(this.formData).then(res => {
              if (res.code === 1) {
                this.$message.success(res.msg || "新增成功！");
                this.closeDialog();
                this.$refs[formData].resetFields();
                this.getTableDataList();
              } else {
                this.$message.error(res.msg || "新增失败！");
              }
            });
          }
        } else {
          // console.log('error submit!!');
          return false;
        }
      });
    },
    openDialog(type) {
      this.dialogFormVisible = true;
      this.dialogTitle = type === 'add' ? '新增' : '编辑'
      this.nowChanceStau = type;
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.dialogTitle = null;
      this.nowChanceStau = '';
    }
  }
};
</script>

<style scoped lang="scss">

  ::v-deep .el-input-group__append {
    background-color: #00a1c200;
    padding: 0 0 0 10px !important;
    border: 0px solid #6ecae3;
    color: #FFF;
    .el-form-item {
      display: flex;
        width: 20% !important;
      .el-form-item__label {
        width: 20% !important;
        min-width: 50px !important;
      }
      .el-form-item__content {
        margin: 0 !important;
        // width: 60% !important;
        .el-select--medium {
          border: 1px solid #6ecae3;
          border-radius: 5px;
          width: 100px !important;
          margin: 0 !important;
          color: #ffffff;
        }
        .el-select .el-input.is-disabled .el-input__inner:hover {
          border: none !important;
      }
      }
    }
  }
  </style>
